define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var meal_id = Fast.api.query("meal_id");
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/meal_rule/index' + location.search,
                    add_url: 'user/meal_rule/add/meal_id/' + meal_id,
                    edit_url: 'user/meal_rule/edit',
                    del_url: 'user/meal_rule/del',
                    multi_url: 'user/meal_rule/multi',
                    import_url: 'user/meal_rule/import',
                    table: 'user_meal_rule',
                },
                queryParams: function (params) {
                    var filter = params.filter ? JSON.parse(params.filter) : {}; //判断当前是否还有其他高级搜索栏的条件
                    var op = params.op ? JSON.parse(params.op) : {};  //并将搜索过滤器 转为对象方便我们追加条件
                    //将需要的参数meal_id 加入到搜索条件中去 要求url传递的参数必须为group_id=XX;可以用 Fast.api.query("group_id")获取到！
                    filter.meal_id= Fast.api.query("meal_id");
                    op.meal_id= "=";  //meal_id的操作方法的为 找到相等的
                    params.filter = JSON.stringify(filter); //将搜索过滤器和操作方法 都转为JSON字符串
                    params.op = JSON.stringify(op);
                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify({url: 'login'});
                    //params.op = JSON.stringify({url: 'like'});
                    return params;
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'tool.name', title: __('Tool_id')},
                        {field: 'number', title: __('Number')},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),'hidden':__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'create_time', title: __('添加时间'), operate:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
